import sys
import scipy
import random
import time

sys.path.append('../../')
import main


class TraceFactory(object):
    'Generate time series traces'


    # MINIMUM FLAT-SEG = 51

    """
    Data with increasingly long flat-segments interspersed by noise:
        -++--++----++--------++
    
    The first flat-line segment is 50 points long, the second is 100, and 
    so forth goemetrically.  
    Between each flat-line segment are 199 (a lovely prime) points 
    of noise. 
    
    Each flat-line segment is at an incrementally larger value.
    """
    def generateTrace0(self):
        flatSegs = [(249, 348), (548, 747), (947, 1346), (1546, 2345),\
                    (2545, 4144), (4344, 7543), (7743, 14142)]

        data = {'dateTime':[], 'kW':[], 'V':[]}
    
        noiseSegment=199
        flatSegment=50
        flatValue=0
        flatValueIncrement=0.2
        while (flatSegment <= 6400):
            for j in range(flatSegment):
                data['kW'].append(flatValue)
                data['V'].append(0)
                data['dateTime'].append(time.strptime("2010-08-23 00:00:00",
                                                      "%Y-%m-%d %H:%M:%S"))
            for j in range(noiseSegment):
                data['kW'].append(flatValue + 2 * random.random() - 1)
                data['V'].append(0)
                data['dateTime'].append(time.strptime("2010-08-23 00:00:00",
                                                      "%Y-%m-%d %H:%M:%S"))
            flatSegment *= 2
            flatValue += flatValueIncrement
        
        data['kW'] = scipy.array(data['kW'])
        data['V'] = scipy.array(data['V'])
    
        return flatSegs, data
    
    
    
    """
    Data without any flat segments:
        ++++++++++++++++++++++++++++++++
    """
    def generateTrace1(self):
        flatSegs = []

        data = {'dateTime':[], 'kW':[], 'V':[]}
        
        for j in range(5000):
                data['kW'].append(random.random())
                data['V'].append(0)
                data['dateTime'].append(time.strptime("2010-08-23 00:00:00",
                                                      "%Y-%m-%d %H:%M:%S"))
        data['kW'] = scipy.array(data['kW'])
        data['V'] = scipy.array(data['V'])
    
        return flatSegs, data
    
    
    """
    Data that is entirely a flat segment:
        --------------------------------
    """
    def generateTrace2(self):
        flatSegs=[(0, 4999)]

        data = {'dateTime':[], 'kW':[], 'V':[]}
        
        for j in range(5000):
                data['kW'].append(0)
                data['V'].append(0)
                data['dateTime'].append(time.strptime("2010-08-23 00:00:00",
                                                      "%Y-%m-%d %H:%M:%S"))
        data['kW'] = scipy.array(data['kW'])
        data['V'] = scipy.array(data['V'])
    
        return flatSegs, data
    
    
    """
    Data that is a very short flat segment amongst noise:
        +++++++++++++++-++++++++++++++++
    
    5e3 data-points in total.  Flat segment between element 2.5e3 and 2.6e3
    """
    def generateTrace3(self):
        flatSegs=[(2500, 2599)]

        data = {'dateTime':[], 'kW':[], 'V':[]}
        
        for j in range(5000):
                data['kW'].append(random.random())
                data['V'].append(0)
                data['dateTime'].append(time.strptime("2010-08-23 00:00:00",
                                                      "%Y-%m-%d %H:%M:%S"))
        for j in range(2500, 2600):
                data['kW'][j] = 0
    
        data['kW'] = scipy.array(data['kW'])
        data['V'] = scipy.array(data['V'])
    
        return flatSegs, data
    
    
    """
    Data that is a very short segment of noise amongst flat segments:
        ---------------+----------------
    
    5e3 data-points in total.  Noise between element 2.5e3 and 2.6e3
    """
    def generateTrace4(self):
        flatSegs=[(0, 2499), (2600, 4999)]

        data = {'dateTime':[], 'kW':[], 'V':[]}
        
        for j in range(5000):
                data['kW'].append(1)
                data['V'].append(0)
                data['dateTime'].append(time.strptime("2010-08-23 00:00:00",
                                                      "%Y-%m-%d %H:%M:%S"))
        for j in range(2500, 2600):
                data['kW'][j] = random.random()
    
        data['kW'] = scipy.array(data['kW'])
        data['V'] = scipy.array(data['V'])
    
        return flatSegs, data
    
    
    """
    Linear data (no flat segments):
           /
          /
         /
        /
    """
    def generateTrace5(self):
        flatSegs=[]

        data = {'dateTime':[], 'kW':[], 'V':[]}
        
        for j in range(5000):
                data['kW'].append(j)
                data['V'].append(0)
                data['dateTime'].append(time.strptime("2010-08-23 00:00:00",
                                                      "%Y-%m-%d %H:%M:%S"))
    
        data['kW'] = scipy.array(data['kW'])
        data['V'] = scipy.array(data['V'])
    
        return flatSegs, data
    
    
if __name__ == "__main__":
        traceFactory = TraceFactory()
        # List containing all generateTrace definitions:
        generateTraceFns = [gen for gen in dir(traceFactory) if\
                            'generateTrace' in gen]
        
        for function in generateTraceFns:
            print "Testing against", function
            expectedFlatSegs, data = eval('traceFactory.' + function)()
            calculatedFlatSegs =\
                main.analysis.featureExtraction.findFlatSegs(data, minSeg=51,
                                                             sampleSeg=10)
            if (expectedFlatSegs != calculatedFlatSegs):
                print "FAIL:"
                print "     expected =", expectedFlatSegs
                print "     calculated =", calculatedFlatSegs
            else: 
                print "SUCCESS!"
